//
//  JHBookWordDetailViewController.m
//  JuniRengLish_iOS
//
//  Created by revogi on 2019/5/9.
//  Copyright © 2019 revogi. All rights reserved.
//

#import "JHBookWordDetailViewController.h"
#import "JHBookUnitDetailViewController.h"
#import "JHBookDataListView.h"

@interface JHBookWordDetailViewController () <UIScrollViewDelegate>

@property (nonatomic, strong) UISegmentedControl *segmentedControl;

@property (nonatomic, strong) UIScrollView * scrollView;
@end

@implementation JHBookWordDetailViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initNav];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:[DJTool imageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"背景1"] forBarMetrics:UIBarMetricsDefault];
}

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, JKScreenW, JKScreenH - kStatusBarAndNavigationBarHeight - kTabbarSafeBottomMargin)];
        _scrollView.contentSize = CGSizeMake(JKScreenW * 2, _scrollView.height);
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        _scrollView.showsHorizontalScrollIndicator = NO;
        
        for (int i = 0; i < 2; i ++)
        {
            JHBookDataListView * listView = [[JHBookDataListView alloc] initWithFrame:CGRectMake(JKScreenW * i, 0, JKScreenW, _scrollView.height)];
            listView.currentVC = self;
            i == 0 ? (listView.isBook = YES) : (listView.isBook = NO);
            listView.requestUrl = self.model.jsonfileurl;
            [_scrollView addSubview:listView];
        }
    }
    return _scrollView;
}

- (void)initNav
{
    NSArray *array = @[@"课文",@"单词"];
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:array];
    segmentedControl.backgroundColor = [UIColor whiteColor];
    segmentedControl.selectedSegmentIndex = 0;
    segmentedControl.frame = CGRectMake((JKScreenW-120)/2,kStatusBarHeight,120 ,40);
    [segmentedControl addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
    self.segmentedControl = segmentedControl;
    self.navigationItem.titleView = segmentedControl;
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.scrollView];
    
    self.navigationItem.rightBarButtonItem=[UIBarButtonItem itemWithBackIconImage:@"删除" setBackTitle:nil target:self action:@selector(detBtntbtn:)];
  
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / JKScreenW;
    self.segmentedControl.selectedSegmentIndex = index;
}

- (void)detBtntbtn:(UIBarButtonItem *)item
{
    NSLog(@"删除");
    
    self.segmentedControl.selectedSegmentIndex == 0 ?
    [[NSNotificationCenter defaultCenter] postNotificationName:@"deleteBookUnitAllData" object:@"book"] : [[NSNotificationCenter defaultCenter] postNotificationName:@"deleteWordUnitAllData" object:@"word"];
}

- (void)change:(UISegmentedControl*)seg
{
    self.scrollView.contentOffset = CGPointMake(JKScreenW * seg.selectedSegmentIndex, 0);
}

@end
